package cn.ncist.controller;

import cn.ncist.common.RespResult;
import cn.ncist.constant.MessageConstant;
import cn.ncist.dto.OrderDto;
import cn.ncist.health_interface.service.OrderService;
import cn.ncist.pojo.Order;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @DubboReference
    private OrderService orderService;

    @PostMapping("/submit")
    public RespResult submit(@RequestBody OrderDto orderDto){
        Integer setmealId = orderDto.getSetmealId();
        log.info("用户预约的信息为：{}",orderDto.toString());
        log.info("套餐预约的信息为：{}",setmealId.toString());

        try {
            Integer orderId = orderService.order(orderDto);
            return new RespResult(true, MessageConstant.ORDER_SUCCESS,orderId);
        } catch (RuntimeException e) {
            if ("验证码错误请重新输入".equals(e.toString())){
                return new RespResult(false, MessageConstant.VALIDATECODE_ERROR);
            }else if ("当日未设置预约信息".equals(e.toString())){
                return new RespResult(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
            }else if("当日的预约人数已满".equals(e.toString())){
                return new RespResult(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
            }else if("该用户在当天有预约".equals(e.toString())){
                return new RespResult(false, MessageConstant.HAS_ORDERED);
            }else if ("请输入验证码".equals(e.toString())){
                return new RespResult(false, MessageConstant.TELEPHONE_VALIDATECODE_NOTNULL);
            }else {
                return new RespResult(false, "预约失败,请联系管理员");
            }
        }
    }

    @PostMapping("/findById")
    public RespResult findById(Integer id){
        try {
            Map<String, Object> map = orderService.findById4Detail(id);
            return new RespResult(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new RespResult(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
